NodeList.prototype.forEach = Array.prototype.forEach;
var imagesArr = [];
// 项目基本信息 - dom 查询
var customerName__contract = document.getElementById('customerName__contract'),
    contacts__contract = document.getElementById('contacts__contract'),
    regionCode__contract = document.getElementById('regionCode__contract'),
    address__contract = document.getElementById('address__contract'),

    receiveAddress__contract = document.getElementById('receiveAddress__contract'),
    receiveContacts__contract = document.getElementById('receiveContacts__contract'),
    phone__contract = document.getElementById('phone__contract'),
    amount__contract = document.getElementById('amount__contract'),
    contractDate__contract = document.getElementById('contractDate__contract');

var path__contract = 'http://pm.ht-home.cn/api/others/getContractInfoForShare.json?projectId='+ window.currentProjectId;
var xhr__contract = new XMLHttpRequest();
xhr__contract.open('GET', path__contract, true);
xhr__contract.send();
xhr__contract.onreadystatechange = function() {
    if (xhr__contract.readyState === 4) {
        if (xhr__contract.status === 200) {

            var jsonData = JSON.parse(xhr__contract.responseText);

            // 项目基本信息 - dom 插入数据

            var cusdata = jsonData.data.customerMsg;
            customerName__contract.innerHTML = typeof (cusdata.customerName) === 'undefined' ? '' : cusdata.customerName;
            contacts__contract.innerHTML = typeof (cusdata.contacts) === 'undefined' ? '' : cusdata.contacts;
            regionCode__contract.innerHTML = typeof (cusdata.regionCode) === 'undefined' ? '' : cusdata.regionCode;
            address__contract.innerHTML = typeof (cusdata.address) === 'undefined' ? '' : cusdata.address;
            var data = jsonData.data;
            receiveAddress__contract.innerHTML = typeof (data.address) === 'undefined' ? '' : data.address;
            receiveContacts__contract.innerHTML = typeof (data.contacts) === 'undefined' ? '' : data.contacts;
            phone__contract.innerHTML = typeof (data.phone) === 'undefined' ? '' : data.phone;
            amount__contract.innerHTML = typeof (data.amount) === 'undefined' ? '' : data.amount;
            contractDate__contract.innerHTML = typeof (data.contractDate) === 'undefined' ? '' : data.contractDate.substring(0, data.contractDate.indexOf(" "));

            function insertImg(classStr) {
                var element = document.getElementsByClassName(classStr),
                    hasPicture = false;
                // 图片 - 数据
                jsonData.data.classList.forEach(function(list, index) {
                    if (typeof(list[classStr]) !== 'undefined') {
                        list[classStr].forEach(function(image, index) {
                            var newNode = document.createElement('li');
                            newNode.className = 'contract-content';
                            var str = '';
                            str += '<img onclick="useHammer_html(event, ' + index + ')" class="' + classStr + 'image" src="';
                            str += image.smallImageUrl;
                            str += '">';
                            imagesArr.push(image.largeImageUrl);
                            newNode.innerHTML = str;
                            element[0].appendChild(newNode);
                            hasPicture = true;
                        });
                    } else {
                        hasPicture = false;
                        console.log('json 文件中 list[classStr] 未定义');
                    }
                });

                // 图片 - 大小
                if (hasPicture) {
                    var images = document.querySelectorAll('.' + classStr + 'image'),
                        vw = document.body.clientWidth,
                        imageWidth = vw * 0.3;
                    marginTop = (vw - imageWidth * 3) / 4;
                    images.forEach(function(image) {
                        image.parentNode.style.height = imageWidth + 'px';
                        image.parentNode.style.marginTop = marginTop + 'px';
                    });
                    images[images.length - 1].parentNode.style.marginBottom = marginTop + 'px';
                }
            }
            insertImg('CONTRACT_HT');
        }
    }
};

function useHammer_html(e, index) {
    var mask = document.getElementById('hammer-mask_html'),
        bg = document.getElementById('hammer-bg_html'),
        box = document.getElementById('hammer-box_html'),
        page = document.getElementById('hammer-page_html'),
        img = document.getElementById('image-big_html');

    window.initHammer_html(mask, bg, box, page, img);
    window.showHammer_html(imagesArr, index);
}
